//
//  EzDigest.h
//  CloudCameraHub
//
//  Created by 刘跃龙 on 11/2/18.
//  Copyright © 2018 Illidan. All rights reserved.
//

#ifndef EzDigest_h
#define EzDigest_h

#include <string>
#include <vector>
#include <map>
#include "EzTools.h"

class EzDigest
{
public:
    typedef struct UserInfo_t{
        std::string username;
        std::string password;
    }UserInfo;
    EZ_SYNTHESIZE(std::string,m_realm,Realm);//服务器端
    EZ_SYNTHESIZE(std::string,m_privateKey,PrivateKey);//服务器端
    EZ_SYNTHESIZE(std::string,m_eTag,ETag);//服务器端
    EZ_SYNTHESIZE(int,m_timeout,Timeout);//服务器端
public:
    EzDigest();
    ~EzDigest();
public://服务器端
    void addUserInfo(UserInfo info);
    void delUserInfo(UserInfo info);
    UserInfo getUserInfo(std::string username);
    bool checkNonce(std::string nonce);
    bool checkAuthenticate(
                           std::string username,
                           std::string password,
                           std::string method,
                           std::string uri,
                           std::string body,
                           std::string realm,
                           std::string nonce,
                           std::string nc,
                           std::string algorithm,
                           std::string cnonce,
                           std::string qop,
                           std::string response
                           );
    std::string getMessageForWWWAuthenticate(bool stale);//WWW-Authenticate //服务器端
    std::string getMessageForAuthenticationInfo(
                                                std::string username,
                                                std::string password,
                                                std::string uri,
                                                std::string body,
                                                std::string realm,
                                                std::string nc,
                                                std::string algorithm,
                                                std::string qop,
                                                std::string cnonce
                                                );//Authentication-Info //服务器端
private:
    std::vector<UserInfo> m_userList;
public:
    static std::string genDigestContent(std::map<std::string,std::string> map,std::string head);
    static std::map<std::string,std::string> parseDigestContent(std::string content);
    static std::string genDigest(
                                 std::string username,
                                 std::string password,
                                 std::string method,
                                 std::string uri,
                                 std::string body,
                                 std::string realm,
                                 std::string nonce,
                                 std::string nc,
                                 std::string algorithm,
                                 std::string cnonce,
                                 std::string qop
                                 );
    static std::string genNonce(std::string eTag,std::string privateKey);
    static std::string genCnonce();
    static std::string genOpaque();
    static std::string getNonceCount(int count);
    static std::string getSupportQop(std::string qop);
public://客户端
    static std::string getMessageForAuthenticate(
                                                 std::string username,
                                                 std::string password,
                                                 std::string method,
                                                 std::string uri,
                                                 std::string body,
                                                 std::string realm,
                                                 std::string nonce,
                                                 int nonceCount,
                                                 std::string algorithm,
                                                 std::string cnonce,
                                                 std::string qop,
                                                 std::string opaque
                                                 );//Authenticate 客户端使用
};

#endif /* EzDigest_h */
